//
//  IdiotView.m
//  UI06_homework
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "IdiotView.h"
#import "BigScrollView.h"

@interface IdiotView ()<UIScrollViewDelegate>

@property(nonatomic, retain)BigScrollView *scrollView;
@property(nonatomic, retain)UIPageControl *pageControl;


@end



@implementation IdiotView

-(void)dealloc{
    [_imagesArray release];
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
}


-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.imagesArray = [NSArray array];
        [self creatSubViews];
    }
    return self;
}

-(void)creatSubViews{
    // 创建scrollView;
    self.scrollView = [[BigScrollView alloc]initWithFrame:self.bounds];
    [self addSubview:self.scrollView];
    [_scrollView release];
    [self.scrollView addPictures:self.imagesArray];
    
    self.scrollView.delegate = self;
    
    // 创建pageControl;
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds )/ 5 * 3, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) / 5 *2)];
    [self addSubview:self.pageControl];
    [_pageControl release];
    
    self.pageControl.numberOfPages = self.imagesArray.count;
//    [self.pageControl.currentPage = 0];
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
}
-(void)pageAction:(UIPageControl *)pageControl{
//    偏移量
    self.scrollView.contentOffset = CGPointMake(pageControl.currentPage *CGRectGetWidth(self.bounds), 0);

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
     NSLog(@"x = %lf, y = %lf", scrollView.contentOffset.x, scrollView.contentOffset.y);
     NSLog(@"width: %lf made:%lf nimeide : %lf", scrollView.contentSize.width, CGRectGetWidth(self.bounds), scrollView.contentSize.width - CGRectGetWidth(self.bounds)+10);
    
    if (self.scrollView.contentOffset.x > (scrollView.contentSize.width - CGRectGetWidth(self.bounds)+30)) {
        // 偏移量最da 时, 跳到第一页
         NSLog(@"qqqx = %lf, y = %lf", scrollView.contentOffset.x, scrollView.contentOffset.y);
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }else if (self.scrollView.contentOffset.x < 0){
        // 偏移量为负数时 , 跳到最后一页
        self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds) * (self.imagesArray.count - 1), 0);
    }
    
    
     // 重置图片大小
    for(UIScrollView *scrollView in [self.scrollView subviews]){
        scrollView.zoomScale = 1.0;
     }

    //图片停留不上啊啊啊啊啊啊啊啊啊, 数组值为空 哪里顺序有问题?!!!啊!!!!
//    if(scrollView.contentOffset.x == 0){
//        scrollView.contentOffset = CGPointMake((self.imagesArray.count) * CGRectGetWidth(self.bounds), 0);
//    } else if (scrollView.contentOffset.x == (self.imagesArray.count + 1) *CGRectGetWidth(self.bounds)){
//        scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
//    }

    
    
    
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.pageControl.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
   
//    if (self.scrollView.contentOffset.x > (scrollView.contentSize.width * (self.imagesArray.count - 1)+10)) {
//        // 偏移量最da 时, 调到最后一页
//        NSLog(@"qqqx = %lf, y = %lf", scrollView.contentOffset.x, scrollView.contentOffset.y);
//        self.scrollView.contentOffset = CGPointMake(0, 0);
//    }else if (self){
//        
//    }

    
    
//    if (self.scrollView.contentOffset.x == (scrollView.contentSize.width - CGRectGetWidth(self.bounds) + 1)) {
        // 偏移量最da 时, 调到最后一页
        
//        self.scrollView.contentOffset = CGPointMake(0, 0);
    
//        [self.scrollView scrollRectToVisible:CGRectMake(CGRectGetWidth(self.bounds) * self.pageControl.numberOfPages, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) animated:NO];
//    }else if (self.scrollView.contentOffset.x / CGRectGetWidth(self.bounds) == self.pageControl.numberOfPages){
        // 偏移量最大时, 调到第一页
//        [self.scrollView scrollRectToVisible:CGRectMake(self.frame.origin.x, self.frame.origin.y, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) animated:NO];
        
//    }
    

    
}


/**重写setter方法.**/
-(void)setImagesArray:(NSArray<ScrollView *> *)imagesArray{
    
    if (_imagesArray != imagesArray) {
        [_imagesArray release];
        
        _imagesArray = [imagesArray retain];
    }
    [self.scrollView addPictures:_imagesArray];
    
    self.pageControl.numberOfPages = _imagesArray.count;
    
   }





















@end
